package com.gaara.ddp;

import java.util.Random;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;


/**
 * @author gaara
 * 
 *  ����Ϸ��
 *
 */
public class GameView extends View implements Runnable{

	Context context;

	public static int SCREEN_W = 480;
	public static int SCREEN_H = 800;

	final int frameRate = 7;
	final int caseWidth = 56;
	final int BODY_W = 8;
	final int BODY_H  = 8;

	final int clearFrameMax = 8;
	final int moveFrameMax = 8;

	int moveFrame,clearFrame;

	boolean isClear,isSelected,isExchange,isReExchange;
	boolean isDown = true;

	//��¼���Ƶ����
	int beginDrawX=-1,beginDrawY=-1;

	//��¼���������
	int currentX = -1,currentY = -1,selectedX = -1,selectedY = -1;

//	Bomb bomb;

	Random radom = new Random();

	Paint paint = new Paint();

	//��������
	final int blockCount = 7;
	//ͼƬԪ��
	private Bitmap[] block = new Bitmap[blockCount];
	//ѡ��ʱ��ͼƬ
	private Bitmap cursor0;

	int[][] body = new int[BODY_W][BODY_H];
	int[][] tempMove = new int[BODY_W][BODY_H];
	int[][] clear = new int[BODY_W][BODY_H];

	public GameView(Context context) {
		super(context);
/*		System.out.println("create");
		this.context = context;
		setFocusable(true);

		paint.setColor(Color.GRAY);
		paint.setTextSize(22);

		beginDrawX = (SCREEN_W-caseWidth*BODY_W)/2;
		System.out.println(beginDrawX);
		beginDrawY = (SCREEN_H-caseWidth *BODY_H)/2;
		System.out.println(beginDrawY);
		loadBitmap();

		bomb = new Bomb(block, SCREEN_W,SCREEN_H);
		System.out.println("create end");*/
	}

	/** ��������λ�� **/
	public Bitmap loadBall(Drawable tile){
		Bitmap bitmap = Bitmap.createBitmap(caseWidth, caseWidth, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		tile.setBounds(0, 0, caseWidth, caseWidth);
		tile.draw(canvas);
		return  bitmap;
	}

	public void loadBitmap(){
		Resources re = this.getResources();
		cursor0 =  loadBall(re.getDrawable(R.drawable.cursor0));
		block[0] = loadBall(re.getDrawable(R.drawable.ball0));
		block[1] = loadBall(re.getDrawable(R.drawable.ball1));
		block[2] = loadBall(re.getDrawable(R.drawable.ball2));
		block[3] = loadBall(re.getDrawable(R.drawable.ball3));
		block[4] = loadBall(re.getDrawable(R.drawable.ball4));
		block[5] = loadBall(re.getDrawable(R.drawable.ball5));
		block[6] = loadBall(re.getDrawable(R.drawable.ball6));
		System.out.println(block[0].getWidth());
		System.out.println(block[0].getHeight());

	}

	public void logic(){
		//�Ƴ���ʧ�Ŀ�
		//	bomb.move();

		if (isClear) {
			clearFrame++;
			if (clearFrame >= clearFrameMax) {
				clearFrame = 0;
				isClear = false;
				isDown = doDown();
			}
		}
		
		if(isDown || isExchange || isReExchange){
			moveFrame++;
		}

		if(isDown){
			if(moveFrame>=moveFrameMax){
				moveFrame = 0;
				isDown = doDown();
				if(isDown==false){
					isClear = checkClear();
				}
			}
		}

		if(isExchange){
			if(moveFrame>=moveFrameMax){
				moveFrame = 0;
				isExchange = false;
				tempMove = new int[BODY_W][BODY_H];
				isClear = checkClear();
				if (isClear ==false) {
					isReExchange = true;
					doExchange();
				}
			}
		}
		
		if (isReExchange) {
			if (moveFrame >= moveFrameMax) {
				tempMove = new int[BODY_W][BODY_H];
				moveFrame = 0;
				isReExchange = false;
			}
		}

	}

	public Boolean doDown(){
		Boolean isDown = false;
		for(int i=0;i<body.length;i++){
			for(int j=body[i].length-1;j>=0;j--){
				tempMove[i][j]=0;
				if(body[i][j]==0){
					isDown = true;
					for(int k=j;k>=0;k--){
						tempMove[i][k] = 2;
						if(k == 0){
							body[i][k] = Math.abs(radom.nextInt()%blockCount)+1 ;
						}
						else {
							body[i][k] = body[i][k-1];
						}
					}
					break;
				}
			}
		}
		return isDown;
	}



	@Override
	protected void onDraw(Canvas canvas) {
		for(int i=0;i<tempMove.length;i++){
			for(int j=0;j<tempMove[i].length;j++){
				switch (tempMove[i][j]) {
				case 2:
					paintBlock(canvas, body[i][j], i * caseWidth, (j - 1)* caseWidth + (moveFrame * frameRate));
					break;
				case 8:
					paintBlock(canvas, body[i][j], i * caseWidth, (j+ 1)* caseWidth - (moveFrame * frameRate));
					break;
				case 4:
					paintBlock(canvas, body[i][j], (i  - 1)* caseWidth+ (moveFrame * frameRate), j* caseWidth );
					break;
				case 6:
					paintBlock(canvas, body[i][j], (i  + 1)* caseWidth- (moveFrame * frameRate), j* caseWidth );
					break;

				default:
					if (clear[i][j] == 0) {
						paintBlock(canvas, body[i][j], i * caseWidth, j* caseWidth);
					}
					break;
				}

			}
		}

		canvas.drawBitmap(cursor0, beginDrawX + currentX*caseWidth, beginDrawY + currentY*caseWidth,paint);

		//	bomb.paint(canvas, paint);
	}



	private void paintBlock(Canvas canvas, int index, int x, int y) {
		index--;
		if (index >= 0 && index < block.length) {
			canvas.save();
			canvas.clipRect(beginDrawX, beginDrawY, beginDrawX + caseWidth* BODY_W, beginDrawY + caseWidth * BODY_H);
			canvas.drawBitmap(block[index], beginDrawX + x, beginDrawY + y,paint);
			canvas.restore();
		}
	}



	@Override
	public boolean onTouchEvent(MotionEvent event) {

		float x = event.getX();
		float y = event.getY();
		//����ǰ��»��߲������򣬻��ߴ��ڸ���״̬ʱ��������Ч
		if(event.getAction() != MotionEvent.ACTION_DOWN 
				|| x<beginDrawX || x>(beginDrawX+BODY_W*caseWidth) || y<beginDrawY || y>(beginDrawY+BODY_H*caseWidth)
				|| isDown || moveFrame!=0){
			return super.onTouchEvent(event);
		}

		//����������ĸ���
		int tempX = (int)(x-beginDrawX)/caseWidth;
		int tempY = (int)(y-beginDrawY)/caseWidth;

		//����Ѿ���ѡ���˾ͽ��н���
		if(isSelected){
			System.out.println("tempx"+tempX+" "+selectedX);
			System.out.println("tempy"+tempY+" "+selectedY);
			if(tempX == selectedX){
				if(tempY>selectedY){
					moveDown();
				}
				else if(tempY<selectedY){
					moveUp();
				}
			}
			else if(tempY == selectedY){
				if(tempX>selectedX){
					moveRight();
				}
				else if(tempX<selectedX){
					moveLeft();
				}
			}
		}
		else {
			isSelected = true;
			currentX = selectedX = tempX;
			currentY = selectedY = tempY;

		}




		return super.onTouchEvent(event);
	}

	public void moveDown(){
		if(currentY == BODY_H-1){
			isSelected = false;
		}
		currentY = ++currentY % BODY_H;
		setExchange();
	}
	public void moveUp(){
		if (currentY == 0) {
			isSelected = false;
		}
		currentY = (--currentY + BODY_H) % BODY_H;
		setExchange();
	}
	public void moveRight(){
		if (currentX == BODY_W-1) {
			isSelected = false;
		}
		currentX = ++currentX % BODY_W;
		setExchange();
	}
	public void moveLeft(){
		if (currentX == 0) {
			isSelected = false;
		}
		currentX = (--currentX + BODY_W) % BODY_W;
		setExchange();
	}

	public void setExchange() {
		if (isSelected) {
			isExchange = true;
			isSelected = false;
			doExchange();
		}
	}

	public void doExchange(){
		if(currentX == selectedX){
			if(currentY-selectedY==-1){
				tempMove[currentX][currentY] = 8;
				tempMove[selectedX][selectedY] = 2;
			}
			if(currentY-selectedY==1){
				tempMove[currentX][currentY] = 2;
				tempMove[selectedX][selectedY] = 8;
			}
		}
		else if(currentY == selectedY){
			if(currentX-selectedX==-1){
				tempMove[currentX][currentY] = 6;
				tempMove[selectedX][selectedY] = 4;
			}
			if(currentX-selectedX==1){
				tempMove[currentX][currentY] = 4;
				tempMove[selectedX][selectedY] = 6;
			}
		}

		int temp = body[selectedX][selectedY];
		body[selectedX][selectedY] = body[currentX][currentY];
		body[currentX][currentY] = temp;
	}

	public boolean checkClear(){
		for(int i=0;i<body.length;i++){
			for(int j=0;j<body[i].length;j++){
				if(j<body[i].length-2 && body[i][j]==body[i][j+1] && body[i][j+1]==body[i][j+2]){
					clear[i][j] = clear[i][j+1] =clear [i][j+2] =1;
				}
				if(i<body.length-2 && body[i][j]==body[i+1][j] && body[i+1][j]==body[i+2][j]){
					clear[i][j] = clear[i+1][j] =clear [i+2][j] =1;
				}
			}			
		}

		boolean isClear = false;
		for(int i=0;i<body.length;i++){
			for(int j=0;j<body[i].length;j++){
				if(clear[i][j]==1){
					isClear = true;
					clear[i][j] = body[i][j] = 0;
				}
			}
		}
		return isClear;
	}

	public void run() {
		logic();
		invalidate();
		postDelayed(this, 50);
	}

}
